package gek.uni.aspirant.uml.utils.providers;

import gek.uni.aspirant.uml.utils.Activator;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Parameter;
import org.eclipse.uml2.uml.internal.impl.ClassImpl;
import org.eclipse.uml2.uml.internal.impl.ModelImpl;
import org.eclipse.uml2.uml.internal.impl.PackageImpl;

public class UmlLabelProvider implements ILabelProvider {

  private Map<ImageDescriptor, Image> imageCache = new HashMap<ImageDescriptor, Image>();

  public UmlLabelProvider() {
  }

  @Override
  public Image getImage(Object element) {
    ImageDescriptor descriptor = null;
    if (PackageImpl.class.equals(element.getClass()))
      descriptor = Activator.getImageDescriptor("package.gif");
    else if (ClassImpl.class.equals(element.getClass()))
      descriptor = Activator.getImageDescriptor("classes.gif");
    else if (ModelImpl.class.equals(element.getClass()))
      descriptor = Activator.getImageDescriptor("umlModel.gif");
    else
      return null;

    Image image = (Image) imageCache.get(descriptor);
    if (image == null) {
      image = descriptor.createImage();
      imageCache.put(descriptor, image);
    }
    return image;
  }

  @Override
  public String getText(Object element) {
    if (element instanceof Model)
      return ((Model) element).toString();
    else if (element instanceof Package)
      return ((Package) element).getName();
    else if (element instanceof Class)
      return ((Class) element).getName();
    else if (element instanceof Operation) {
      StringBuilder bldr = new StringBuilder();
      for (Iterator<Parameter> i = ((Operation) element).getOwnedParameters()
          .iterator(); i.hasNext();) {
        Parameter param = i.next();
        bldr.append(param.getName()).append(":");
        if (param.getType() != null)
          bldr.append(param.getType().getName());
        else
          bldr.append("void");
        if (i.hasNext())
          bldr.append("; ");
      }
      return ((Operation) element).getName() + "(" + bldr.toString() + ")";
    } else
      return null;
  }

  @Override
  public void addListener(ILabelProviderListener listener) {

  }

  @Override
  public void dispose() {

  }

  @Override
  public boolean isLabelProperty(Object element, String property) {
    return false;
  }

  @Override
  public void removeListener(ILabelProviderListener listener) {

  }

}
